// 导包
import org.locationtech.proj4j.*;

import java.math.BigDecimal;

// 工具类定义
public class CoordinatesUtil {
    /**
     * WGS84->2000国家大地坐标系(CGCS2000)
     *
     * @param longitude WGS84经度值
     * @param latitude WGS84纬度值
     * @return x,y CGCS2000经度值,CGCS2000纬度值
     */
    public static String WGS84ToCGCS2000(double longitude, double latitude) {
        CRSFactory crsFactory = new CRSFactory();
        CoordinateReferenceSystem WGS84 = crsFactory.createFromName("epsg:4326");
        // 根据经度范围确定转换标准,具体EPSG定义参考https://epsg.io
        String degree3EPSG = "epsg:4528";
        if (longitude < 76.3) {
            // CGCS2000 / 3-degree Gauss-Kruger zone 25
            degree3EPSG = "epsg:4513";
        } else if (longitude >= 76.3 && longitude < 79.3) {
            // CGCS2000 / 3-degree Gauss-Kruger zone 26
            degree3EPSG = "epsg:4514";
        } else if (longitude >= 79.3 && longitude < 82.3) {
            // CGCS2000 / 3-degree Gauss-Kruger zone 27
            degree3EPSG = "epsg:4515";
        } else if (longitude >= 82.3 && longitude < 85.3) {
            // CGCS2000 / 3-degree Gauss-Kruger zone 28
            degree3EPSG = "epsg:4516";
        } else if (longitude >= 85.3 && longitude < 88.3) {
            // CGCS2000 / 3-degree Gauss-Kruger zone 29
            degree3EPSG = "epsg:4517";
        } else if (longitude >= 88.3 && longitude < 91.3) {
            // CGCS2000 / 3-degree Gauss-Kruger zone 30
            degree3EPSG = "epsg:4518";
        } else if (longitude >= 91.3 && longitude < 94.3) {
            // CGCS2000 / 3-degree Gauss-Kruger zone 31
            degree3EPSG = "epsg:4519";
        } else if (longitude >= 94.3 && longitude < 97.3) {
            // CGCS2000 / 3-degree Gauss-Kruger zone 32
            degree3EPSG = "epsg:4520";
        } else if (longitude >= 97.3 && longitude < 100.3) {
            // CGCS2000 / 3-degree Gauss-Kruger zone 33
            degree3EPSG = "epsg:4521";
        } else if (longitude >= 100.3 && longitude < 103.3) {
            // CGCS2000 / 3-degree Gauss-Kruger zone 34
            degree3EPSG = "epsg:4522";
        } else if (longitude >= 103.3 && longitude < 106.3) {
            // CGCS2000 / 3-degree Gauss-Kruger zone 35
            degree3EPSG = "epsg:4523";
        } else if (longitude >= 106.3 && longitude < 109.3) {
            // CGCS2000 / 3-degree Gauss-Kruger zone 36
            degree3EPSG = "epsg:4524";
        } else if (longitude >= 109.3 && longitude < 112.3) {
            // CGCS2000 / 3-degree Gauss-Kruger zone 37
            degree3EPSG = "epsg:4525";
        } else if (longitude >= 112.3 && longitude < 115.3) {
            // CGCS2000 / 3-degree Gauss-Kruger zone 38
            degree3EPSG = "epsg:4526";
        } else if (longitude >= 115.3 && longitude < 118.3) {
            // CGCS2000 / 3-degree Gauss-Kruger zone 39
            degree3EPSG = "epsg:4527";
        } else if (longitude >= 118.3 && longitude < 121.3) {
            // CGCS2000 / 3-degree Gauss-Kruger zone 40
            degree3EPSG = "epsg:4528";
        } else if (longitude >= 121.3 && longitude < 124.3) {
            // CGCS2000 / 3-degree Gauss-Kruger zone 41
            degree3EPSG = "epsg:4529";
        } else if (longitude >= 124.3 && longitude < 127.3) {
            // CGCS2000 / 3-degree Gauss-Kruger zone 42
            degree3EPSG = "epsg:4530";
        } else if (longitude >= 127.3 && longitude < 130.3) {
            // CGCS2000 / 3-degree Gauss-Kruger zone 43
            degree3EPSG = "epsg:4531";
        } else if (longitude >= 130.3 && longitude < 133.3) {
            // CGCS2000 / 3-degree Gauss-Kruger zone 44
            degree3EPSG = "epsg:4532";
        } else if (longitude >= 133.3) {
            // CGCS2000 / 3-degree Gauss-Kruger zone 45
            degree3EPSG = "epsg:4533";
        }
        CoordinateReferenceSystem CGCS2000 = crsFactory.createFromName(degree3EPSG);
        // 定义转换Factory
        CoordinateTransformFactory ctFactory = new CoordinateTransformFactory();
        CoordinateTransform wgs84ToCGCS2000 = ctFactory.createTransform(WGS84, CGCS2000);
        // 转换结果数据定义
        ProjCoordinate result = new ProjCoordinate();
        // 转换方法调用
        wgs84ToCGCS2000.transform(new ProjCoordinate(longitude, latitude), result);
        // 输出转换值: x前俩位表示的3度经度分带的中国区域内编号, x->longitude, y->latitude
        return BigDecimal.valueOf(result.x) + "," + BigDecimal.valueOf(result.y);
    }

    /**
     * 2000国家大地坐标系(CGCS2000)->WGS84
     * @param longitudeX CGCS2000经度值
     * @param latitudeY CGCS2000纬度值
     * @return x,y WGS84经度值,WGS84纬度值
     */
    public static String CGCS2000ToWGS84(double longitudeX, double latitudeY) {
        String degree3No = String.valueOf(BigDecimal.valueOf(longitudeX)).substring(0, 2);
        CRSFactory crsFactory = new CRSFactory();
        CoordinateReferenceSystem WGS84 = crsFactory.createFromName("epsg:4326");
        // 根据精度范围换算使用坐标标准
        String degree3EPSG = "epsg:4528";
        switch (degree3No) {
            case "25":
                degree3EPSG = "epsg:4513";
                break;
            case "26":
                degree3EPSG = "epsg:4514";
                break;
            case "27":
                degree3EPSG = "epsg:4515";
                break;
            case "28":
                degree3EPSG = "epsg:4516";
                break;
            case "29":
                degree3EPSG = "epsg:4517";
                break;
            case "30":
                degree3EPSG = "epsg:4518";
                break;
            case "31":
                degree3EPSG = "epsg:4519";
                break;
            case "32":
                degree3EPSG = "epsg:4520";
                break;
            case "33":
                degree3EPSG = "epsg:4521";
                break;
            case "34":
                degree3EPSG = "epsg:4522";
                break;
            case "35":
                degree3EPSG = "epsg:4523";
                break;
            case "36":
                degree3EPSG = "epsg:4524";
                break;
            case "37":
                degree3EPSG = "epsg:4525";
                break;
            case "38":
                degree3EPSG = "epsg:4526";
                break;
            case "39":
                degree3EPSG = "epsg:4527";
                break;
            case "40":
                degree3EPSG = "epsg:4528";
                break;
            case "41":
                degree3EPSG = "epsg:4529";
                break;
            case "42":
                degree3EPSG = "epsg:4530";
                break;
            case "43":
                degree3EPSG = "epsg:4531";
                break;
            case "44":
                degree3EPSG = "epsg:4532";
                break;
            case "45":
                degree3EPSG = "epsg:4533";
                break;
        }
        CoordinateReferenceSystem CGCS2000 = crsFactory.createFromName(degree3EPSG);
        CoordinateTransformFactory ctFactory = new CoordinateTransformFactory();
        // 转换区别实际就这一行创建转换器的代码,前后顺序变了
        CoordinateTransform cgcs2000ToWGS84 = ctFactory.createTransform(CGCS2000, WGS84);
        ProjCoordinate result = new ProjCoordinate();
        cgcs2000ToWGS84.transform(new ProjCoordinate(longitudeX, latitudeY), result);
        return BigDecimal.valueOf(result.x) + "," + BigDecimal.valueOf(result.y);
    }

    // 转换测试, 实际作为工具类使用main方法直接删掉
    public static void main(String[] args) {
        System.out.println("Start Coordinate: ");

        System.out.println(WGS84ToCGCS2000(121.495191023,31.2419292248));
//        System.out.println(WGS84ToCGCS2000(113.974988,22.563086));
//        System.out.println(CGCS2000ToWGS84(40445244.52773628,3510826.8681515823));
//        System.out.println(CGCS2000ToWGS84(38497427.52755689,2496153.513361627));

        System.out.println("Coordinated.");
    }
}